<?php
require_once("../../../config.inc.php");
$isReloadable=true;
require_once(SERVER_FS_ROOT."/app/_inc/include.php");
require_once(SERVER_FS_ROOT."/app/util/imex/_inc/importExport.func.php");

$errMsg = sxREQ('msg');

$objUser = $_SESSION[USEROBJ];

$nomeFile = 'exp'.uniqid(date('ymd'));
$aExpTab = array();
$errMsg .= generaFile($nomeFile, $aExpTab);

$aOpz = array("kMenu"=>"uti.rel", "isReloadable"=>$isReloadable);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript">
</script>
  
<?php
echo(bodyHTML($aOpz));
echo("<table width=\"100%\" cellpadding=\"03\" cellspacing=\"0\">\n<tr>\n<td>\n");
//output comandi
$aCmd = array(
	"download"=>array("pag"=>"javascript:document.location.href='download.php?nf=$nomeFile&xsn=".XSN."';",
						"title"=>_t("download"),
						"text"=>_t("download")
						),
	"back"=>array()
	);
echo(commandsHTML($aCmd));	
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" colspan ='2'>Release delivery</td>" .
		"</tr>\n");
		echo("<tr>" .
			"<td class=\"tabh01\" width='10%'>Item type</td>" .
			"<td class=\"tabh01\" width='90%'>Number of items</td>" .
			"</tr>\n");
foreach($aExpTab as $tabRef => $aTabRef) {
		echo("<tr>" .
			"<td class=\"text01\">$tabRef </td>" .
			"<td class=\"text01\">".count($aTabRef)."</td>" .
			"</tr>\n");
}
echo("</table>");
echo(commandsHTML($aCmd));
echo("</form>");
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());
exit();

function generaFile($nomeFile, &$aExpTab){
	global $objSDAO;
	// load array import
	$sSQL = "SELECT entity FROM entity ORDER BY entity";
	$objSDAO->query($sSQL);
	while($row = $objSDAO->fetch()) {
		$msg = loadExpTab('entity', $row['entity'], $aExpTab );
		if ($msg != '') { return $msg; } 
	}
	$sSQL = "SELECT process_unit FROM process_unit ORDER BY process_unit";
	$objSDAO->query($sSQL);
	while($row = $objSDAO->fetch()) {
		$msg = loadExpTab('process_unit', $row['process_unit'], $aExpTab );
		if ($msg != '') { return $msg; } 
	}
	$sSQL = "SELECT * FROM relationship ORDER BY parent_entity, child_entity, number";
	$objSDAO->query($sSQL);
	while($row = $objSDAO->fetch()) {
		if (isset($aExpTab['entity'][$row['parent_entity']])
		and isset($aExpTab['entity'][$row['child_entity']])) {
			$msg = loadExpTab('relationship', $row['parent_entity'].'@'.$row['child_entity'].'@'.$row['number'], $aExpTab );
			if ($msg != '') { return $msg; } 
		}
	}
	$sSQL = "SELECT * FROM menu_page ORDER BY module, menu_page";
	$objSDAO->query($sSQL);
	while($row = $objSDAO->fetch()) {
		$msg = loadExpTab('menu_page', $row['module'].'@'.$row['menu_page'], $aExpTab );
		if ($msg != '') { return $msg; } 
	}
	$sSQL = "SELECT * FROM library ORDER BY library";
	$objSDAO->query($sSQL);
	while($row = $objSDAO->fetch()) {
		$msg = loadExpTab('library', $row['library'], $aExpTab );
		if ($msg != '') { return $msg; } 
	}
	//dbg_("export ".print_r($aExpTab,1));
	//crea dir temporanea
 	if (!is_dir(SERVER_FS_TEMP."/imex")) {
 		mkdir(SERVER_FS_TEMP."/imex",0775);
 	}
 	if ($dh = opendir(SERVER_FS_TEMP."/imex")) { //delete old file
 		$ymdLimit = date('ymd',time()-3600*48);
	  	while ($file = readdir($dh)) {
	      if (substr($file,0,3)=='exp' and substr($file,3,6)< $ymdLimit) {
	      	unlink(SERVER_FS_TEMP."/imex/".$file);
	      }
	   }
	   closedir($dh);
	}
 	
 	if (!is_dir(SERVER_FS_TEMP."/imex")) {
 		return _t("Unable to create directory %s",SERVER_FS_TEMP."/imex");
 	}
 	// create  file and header file
	$sFileHeader = str_pad("POLARIS-MB ".date("Y-m-d H:i:s")." ".$objSDAO->dbcPar['dbcType']." ".
		$objSDAO->dbcPar['host']." ".$objSDAO->dbcPar['dbName']." ".
		$_SERVER['SERVER_NAME']." ".$_ENV['COMPUTERNAME'],128);
 	$serTab = serialize($aExpTab);
	if(!file_put_contents(SERVER_FS_TEMP."/imex/$nomeFile.txt", $sFileHeader.$serTab)) {
 		return _t("Impossible to create il file %s",SERVER_FS_TEMP."/imex/$nomeFile.txt");
	}
	//dbg_("aExpTab=".print_r($aExpTab,1));
	return "";
}


?>
